<?php
/*error_reporting(E_ALL);
#####################################
# 终端修改订单模块 Le.Je.2013.03.19 #
#####################################
*/
if (!class_exists("go_sql")){
include("/var/www/html/open/conf/wsdl.le.je");
  $le=new go_sql();
  $le->connect(); 
}

if (!class_exists("pgconn")){
  require("/var/www/html/open/conf/pg.le.je");
  $lee = new pgconn();
  $lee->getConn();
}

class terminal_change {
	public $id;
	public $ordernum; //订单号
	public $dstatus; //状态
	public $reason; //订单变动详细原因以及订单变动细节
	public $dadmin; //操作人员 记录 权限判定
	public $lid; //景点ID（或大类商品ID）
	public $tid; //门票ID（或小类商品ID）
	public $num; //撤销或修改（撤销为0）
	
	//const Table1="uu_jq_order"; //订单表
	const Table2="uu_ss_order"; //订单表分销
	const JqTable="uu_land";
    const TicketTable="uu_jq_ticket";
	const DTable="uu_distributor";
	const LirunTable="uu_fx_lirun";
	const MemberTable="uu_member";
	const PMoneyTable="uu_p_money";
	const DMoneyTable="uu_d_money";
		
	function __construct($id, $ordernum, $dstatus, $reason, $dadmin, $lid, $tid, $num){
		$this->id=$id;
		$this->ordernum=$ordernum;
		$this->Table=self::Table2;	

		$this->dstatus=$dstatus;
		if ($this->dstatus!=1 && $this->dstatus!=2) $this->halt(107); 
		$this->reason=htmlspecialchars($reason);
		$this->dadmin=$dadmin;
		$this->lid=$lid;
		$this->tid=$tid;
		$this->num=$num;
		if ($this->num<0) $this->halt(107); 
		$this->status=($this->num==0)?6:5;

		if (!$this->authrize()) $this->halt(101);
		if (!$this->auth_status()) $this->halt(111);
		if ($this->dstatus==1) $this->Modify_Order_Data();
		else $this->NO_Order_Change();
	}

   //错误
   public function halt($n){
	    switch($n){
			case 101: $msg="权限不够";break;
			case 102: $msg="数据已超出最大容量";break;
			case 103: $msg="数据库服务出错";break;
			case 104: $msg="无此产品";break;
			case 106: $msg="时间出错";break;
			case 107: $msg="传入的参数有误";break;
			case 108: $msg="终端无法连接";break;
			case 109: $msg="无此订单";break;
			case 110: $msg="修改订单人数超出";break;
			case 111: $msg="记录状态出错";break;
			default:$msg="未知错误";break;
			}
	 	printf("错误发生:<br>%s\n",$msg);
		exit;
   }
	
   //验证身份
	public function authrize(){
		$str="select id from uu_ss_order where member='".$this->dadmin."' and ordernum='".$this->ordernum."' limit 1";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$admin_qx=$GLOBALS['le']->f('id');
		
	    if ($admin_qx) {
			return true;
		}else{
			return false;
		}
	}

   //状态验证
	public function auth_status(){
		$str="select dstatus from uu_order_terminal_change where id=".$this->id;
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$dstatus=$GLOBALS['le']->f('dstatus');
		
	    if ($dstatus==0) {
			return true;
		}else{
			return false;
		}
	}

   //订单信息
	public function get_order_info(){
		$str="select * from ".$this->Table." where ordernum='".$this->ordernum."' limit 1";
        $GLOBALS['le']->query($str);
		if (!$r=$GLOBALS['le']->fetch_assoc()) $this->halt(109);
		return $r;
	}

	//修改订单数据
	public function Modify_Order_Data(){
		//检测是否有产品
		$str="select id from ".self::JqTable." where id=".$this->lid." limit 1";
		$GLOBALS['le']->query($str);
		if (!$GLOBALS['le']->fetch_assoc()) $this->halt(104);

		$str="select id from ".self::TicketTable." where landid=".$this->lid." and id=".$this->tid." limit 1";
        $GLOBALS['le']->query($str);
		if (!$GLOBALS['le']->fetch_assoc()) $this->halt(104);
		
		//订单退款
		$r=$this->get_order_info();
		if ($this->num>=$r['tnum']) $this->halt(110);
		//$this->num剩余的数量$back_num退款数量
		$back_num=($this->num==0)?$r['tnum']:($r['tnum']-$this->num);
		$dreason=($this->num==0)?"取消":"修改";
		    
		$totalmoney=$r['tprice']*$r['tnum'];
		//构造SQL
		$totalmoney1=$r['totalmoney'];

		//虚拟账户退款
		$totalmoney=($totalmoney!=$totalmoney1)?$totalmoney1:$totalmoney;
		//单价
		$Pprice=$totalmoney/$r['tnum'];
		//供应商改撤单不收取手续费
		$lm=$Pprice*$back_num;
		
		if (($r['paymode']==0 || $r['paymode']==1) && $lm>0){
			//分销商+
			$str="update pft_member_money set amoney=amoney+$lm where fid='".$r['member']."' limit 1";
			$GLOBALS['le']->query($str);				
			
			$str="select amoney from pft_member_money where fid='".$r['member']."'";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$l_money=$GLOBALS['le']->f('amoney');
			$str="insert pft_member_journal set fid='".$r['member']."',dmoney=$lm,lmoney=$l_money,orderid='".$this->ordernum."',daction=0,rectime=now(),dtype=1,ptype=0";
			$GLOBALS['le']->query($str);
			
			//供应商-
			$str="update pft_member_money set amoney=amoney-$lm where fid='".$r['aid']."' limit 1";
			$GLOBALS['le']->query($str);				
			
			$str="select amoney from pft_member_money where fid='".$r['aid']."'";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$l_money=$GLOBALS['le']->f('amoney');
			$str="insert pft_member_journal set fid='".$r['aid']."',dmoney=$lm,lmoney=$l_money,orderid='".$this->ordernum."',daction=1,rectime=now(),dtype=1,ptype=0";
			$GLOBALS['le']->query($str);

		}elseif($r['paymode']==2 && $lm>0) {
			//处理退信用额度
			//分销商+
			$str="update pft_member_credit set kmoney=kmoney+$lm where fid='".$r['member']."' and aid='".$r['aid']."' limit 1";
			$GLOBALS['le']->query($str);				
			
			$str="select kmoney from pft_member_credit where fid='".$r['member']."' and aid='".$r['aid']."' limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$l_money=$GLOBALS['le']->f('kmoney');
			$str="insert pft_member_journal set fid='".$r['member']."',dmoney=$lm,lmoney=$l_money,orderid='".$this->ordernum."',daction=0,rectime=now(),dtype=1,ptype=2";
			$GLOBALS['le']->query($str);
		}elseif ($lm<0){
			$this->halt(107);
		}


		$str="update ".$this->Table." set status=".$this->status.",totalmoney=totalmoney-$lm,tnum=".$this->num." where ordernum='".$this->ordernum."' limit 1";
		$GLOBALS['le']->query($str);
		
		$str="update uu_order_terminal_change set dstatus=".$this->dstatus.",reason='".$this->reason."',dadmin='".$this->dadmin."',dtime=now() where id=".$this->id;
		$GLOBALS['le']->query($str);

		$pstr="update order_info set flags=1,order_state=1 where flags=3 and id=".$this->ordernum;
		$GLOBALS['lee']->pgquery($pstr);
		
		$str="select aids_money from uu_order_fx_details where orderid='".$this->ordernum."' limit 1";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$aids_money=$GLOBALS['le']->f('aids_money');
		$num1=$r['tnum'];
		$num2=$num1-$this->num;
		
		if ($aids_money){
			$a_a_money=unserialize($aids_money);
			foreach ($a_a_money as $k=>$v){
				$fh=$v[2]?'-':'+';
				$daction=$v[2]?1:0;
				$v[3]=floor($v[3]*$num2/$num1);
				if ($v[1]==0) {
					//处理退信用额度
					$str="update pft_member_credit set kmoney=kmoney$fh".$v[3]." where fid='".$v[0]."' and aid='".$v[4]."' limit 1";
					$GLOBALS['le']->query($str);
					
					$str="select kmoney from pft_member_credit where fid='".$v[0]."' and aid='".$v[4]."' limit 1";
					$GLOBALS['le']->query($str);
					$GLOBALS['le']->fetch_assoc();
					$l_money=$GLOBALS['le']->f('kmoney');
					$str="insert pft_member_journal set fid='".$v[0]."',dmoney=".$v[3].",lmoney=$l_money,orderid='".$this->ordernum."',daction=$daction,rectime=now(),dtype=0,ptype=2";
					$GLOBALS['le']->query($str);
				
				}elseif ($v[1]==1){
					//处理资金账户
					$str="update pft_member_money set amoney=amoney$fh".$v[3]." where fid='".$v[0]."' limit 1";
					$GLOBALS['le']->query($str);				
					
					$str="select amoney from pft_member_money where fid='".$v[0]."'";
					$GLOBALS['le']->query($str);
					$GLOBALS['le']->fetch_assoc();
					$l_money=$GLOBALS['le']->f('amoney');
					$str="insert pft_member_journal set fid='".$v[0]."',dmoney=".$v[3].",lmoney=$l_money,orderid='".$this->ordernum."',daction=$daction,rectime=now(),dtype=0,ptype=0";
					$GLOBALS['le']->query($str);		
				}		
			}
		}

		//修改订单
		echo "<script>javascript:history.back(-1);</script>";
		
	}
	public function NO_Order_Change(){
		$r=$this->get_order_info();
		$str="update uu_order_terminal_change set dstatus=".$this->dstatus.",reason='".$this->reason."',dadmin='".$this->dadmin."',dtime=now() where id=".$this->id;
		$GLOBALS['le']->query($str);
		$pstr="update order_info set flags=1,order_state=1,tnum='".$r['tnum']."' where flags=3 and id=".$this->ordernum;
		$GLOBALS['lee']->pgquery($pstr);
		echo "<script>javascript:history.back(-1);</script>";
	}	
}



$id       =$_REQUEST['Did'];
$ordernum =$_REQUEST['Dordernum'];
$dstatus  =$_REQUEST['Ddstatus'];
$reason   =$_REQUEST['Dreason'];
$dadmin   =$_REQUEST['Ddadmin'];
$lid      =$_REQUEST['Dlid'];
$tid      =$_REQUEST['Dtid'];
$num      =$_REQUEST['Dnum'];

$Ti=new terminal_change($id, $ordernum, $dstatus, $reason, $dadmin, $lid, $tid, $num);